package controllers.listeners.semrep;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JOptionPane;

import controllers.MainModuleController;

import models.SemRepFileModel;
import models.SemRepModel;
import objects.Constituent;
import views.MainPanel;

/* Opens the chosen semantic representation file */
/* actionPerformed when you click on the "Open" button */
public class OpenSemRepActionListener implements ActionListener{
	
	private MainPanel view;
	
	public OpenSemRepActionListener() {
		view = null;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Constituent constituent = null;
		File file = null;
		
		SemRepFileModel.setToSave(false);
		SemRepFileModel.readNewFile();
		file = SemRepFileModel.getFile();
		
		if (file == null) {
			return;
		}
		
		try {
			SemRepModel.read();
			constituent = SemRepModel.getConstituent();
		}
		catch (Exception ex) {
			JOptionPane.showMessageDialog(null, "The file could not be opened. Please try again.", "Error", JOptionPane.ERROR_MESSAGE);
		}
		
		view.clearPanels();  
		view.setConstituent(constituent);
		view.setCurrentFile(file);
		view.loadSemantic(MainModuleController.setTextAreaSemantic(constituent, view));
		view.revalidate();
	}

	public void setView(MainPanel view){
		this.view = view;
	}
	
}
